package edu.base.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import edu.base.util.StatusUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

/**
 * 封装高复用的响应对象,为了判断逻辑返回状态方便
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class HigherResponse<T> {
	
	private Integer status;	
	private T data;
	private String msg;
	
	//构造器
	private HigherResponse(Integer status,T data) {
		this.status = status;
		this.data = data;
	}
	private HigherResponse(Integer status,String msg) {
		this.status = status;
		this.msg = msg;
	}
	
	// 成功 提供对外公开的方法
	@JsonIgnore					//不用转为json格式
	public boolean isResponseSuccess() {
		return this.status == StatusUtil.SUCCESSSTATUS;
	}
	/*
	 *  成功 提供data方法
	 *  静态加载<T>
	 */
	public static <T> HigherResponse<T> getSuccessRespon(T data){
		return new HigherResponse<T>(StatusUtil.SUCCESSSTATUS, data);
	}
	/*
	 *  成功 提供成功信息msg
	 *  static加载<T>
	 */
	public static <T> HigherResponse<T> getSuccessRespon(String msg){
		return new HigherResponse<T>(StatusUtil.SUCCESSSTATUS, msg);
	}
	
	
	// 失败 提供对外公开的方法
	@JsonIgnore							
	public boolean isResponseFailed() {
		return this.status == StatusUtil.FAILEDSTATUS;
	}
	/*
	 *  失败 提供错误信息msg
	 *  static加载<T>
	 */
	public static <T> HigherResponse<T> getFailedRespon(String msg){
		return new HigherResponse<T>(StatusUtil.FAILEDSTATUS, msg);
	}
	
}
